import Vue from 'vue'
import Router from 'vue-router'
// import { constantRouterMap } from './router.config.js'

// hack router push callback
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(Router)
import mineRouter from './modules/mine'
import newsRouter from './modules/news'
import teachersRouter from './modules/teachers'
import goodsRouter from './modules/goods'
import watchRouter from './modules/watch'

const constantRouterMap = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: { title: '登录' }
  },
  {
    path: '/',
    component: () => import('@/views/home/index'),
    meta: { title: '首页', keepAlive: false }
  },
  {
    path: '/home',
    component: () => import('@/layout/index'),
    redirect: '/course-home',
    meta: {
      title: '科目首页',
      keepAlive: false
    },
    children: [
      {
        path: '/course-home',
        name: 'CourseHome',
        component: () => import('@/views/course-home/index'),
        meta: { title: '科目首页', keepAlive: false }
      },
      {
        path: '/exams',
        name: 'Exams',
        component: () => import('@/views/exams/index'),
        meta: { title: '题库', keepAlive: false }
      },
      {
        path: '/news',
        name: 'News',
        component: () => import('@/views/news/index'),
        meta: { title: '发现', keepAlive: false }
      },
      {
        path: '/mine',
        name: 'Mine',
        component: () => import('@/views/mine/index'),
        meta: { title: '我的', keepAlive: false }
      }
    ]
  }
]

export const asyncRoutes = []
  .concat(mineRouter)
  .concat(newsRouter)
  .concat(teachersRouter)
  .concat(goodsRouter)
  .concat(watchRouter)
// export const asyncRoutes = [...mineRouter, ...newsRouter, ...teachersRouter]

const createRouter = () =>
  new Router({
    mode: 'history', // 如果你是 history模式 需要配置vue.config.js publicPath
    // base: process.env.BASE_URL,
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap.concat(asyncRoutes)
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
